<?php

class ParseXML
{
	// singleton instance 
	private static $instance_;
	
	// Objeto que irá contêr todas as propriedades do OA.
	private $learningObject_;
	
	// Objeto que irá manipular o xml.
	private $xml_;

	// private constructor function 
	// to prevent external instantiation 
	private function __construct( $fileName ) 
	{
		
		// Cria o manipulador do XML.	
		$this->xml_ = simplexml_load_file( ".\\objects\\$fileName.xml" );
		//$content = utf8_encode(file_get_contents(".\\objects\\$fileName.xml"));
		//$xml = simplexml_load_string($content);

		$this->learningObject_ = new LearningObject();
		$this->createObject();
	} 

	// getInstance method 
	public static function getInstance( $fileName ) 
	{ 
		if(!self::$instance_)  
		  self::$instance_ = new self( $fileName ); 

		return self::$instance_; 
	}

	public function getObject()
	{
		return $this->learningObject_;
	}
	
	// Função que traduz o xml em um objeto.
	private function createObject()
	{
		$this->getGeneralProperties();
		$this->getPagesProperties();
	}
	
	// Busca e armazena as propriedades gerais do objeto.	
	private function getGeneralProperties()
	{
		// Seta o nome do objeto.
		$general = $this->xml_->Contents->General;
		$name = $general->name;
		$this->learningObject_->setName( $name );
		
		// Seta o header do objeto.
		$headerXML = $this->xml_->Contents->General->Header;
		$header = new SimpleImage();
		$header->setProperties( $headerXML->extension, $headerXML->path, $headerXML->width, $headerXML->height );
		$this->learningObject_->setHeader( $header );
		
		// Seta o footer do objeto.
		$footerXML = $this->xml_->Contents->General->Footer;
		$footer = new SimpleImage();
		$footer->setProperties( $footerXML->extension, $footerXML->path, $footerXML->width, $footerXML->height );
		$this->learningObject_->setFooter( $footer );
		
		$this->getCreditsProperties();
		$this->getMenuProperties();
	}
	
	// Busca e armazena as propriedades dos créditos.
	private function getCreditsProperties()
	{
		// Gera o objeto que irá conter as informações dos créditos.
		$creditsXML = $this->xml_->Contents->General->Credits;
		$titleXML = $creditsXML->Title;
		$title = new SimpleText();
		$title->setProperties( $titleXML->text, $titleXML->font, $titleXML->size );
		
		$contributionsXML = $creditsXML->Contribution;
		$contributions = array();
		foreach( $contributionsXML as $c )
		{
			$contrTemp = new Contribution();
			$contrTemp->setProperties( $c->id, $c->name, $c->role, $c->title, $c->entity );
			$contributions[] = $contrTemp;
		}
		
		// Seta os créditos do objeto.
		$credits = new Credits();
		$credits->setProperties( $title, $contributions );
		$this->learningObject_->setCredits( $credits );
	}
	
	// Busca e armazena as propriedades do menu.
	private function getMenuProperties()
	{
		// Gera o array com os links.
		$menuXML = $this->xml_->Contents->General->Menu->Title;
		$links = array();
		foreach( $menuXML as $m )
		{
			$linkTemp = new MainLink();
			$linkTemp->setProperties( $m->id, $m->text, $m->idlinkpage );
			$subLinksXML = $m->SubTitle;
			$subLinks = array();
			
			// Gera o array com os sub-links.
			foreach( $subLinksXML as $sl )
			{
				$subLinkTemp = new Link();
				$subLinkTemp->setProperties( $sl->id, $sl->text, $sl->idlinkpage );
				$subLinks[] = $subLinkTemp;
			}
			$linkTemp->setSubLinks( $subLinks );
			$links[] = $linkTemp;
		}
		
		// Seta o menu do objeto.
		$menu = new Menu();
		$menu->setLinks( $links );
		$this->learningObject_->setMenu( $menu );
	}
	
	// Busca e armazena as propriedades das páginas.
	private function getPagesProperties()
	{
		$pagesXML = $this->xml_->Contents->Pages->Page;
		$pages = array();
		
		// Gera o array com as páginas.
		foreach( $pagesXML as $p )
		{
			$pageTemp = new Page();
			$pageTemp->setId( $p->id );
			$pageTemp->setName( $p->name );
			$pageTemp->setTitle( $p->title );
			
			$contentsXML = $p->Content;
			$contentsTemp = array();
			
			//Gera o array com os conteúdos da páginas.
			foreach( $contentsXML->children() as $c )
			{
				$content = new Content();
				switch( $c->getName() )
				{
					case "Text":
						$content = new SimpleText();
						$content->setId( $c->id );
						$content->setProperties( $c->text, $c->font, $c->size );
						break;
					case "Image";
						$content = new SimpleImage();
						$content->setId( $c->id );
						$content->setAllProperties( $c->extension, $c->path, $c->width, $c->height, $c->legend );
						break;	
					case "Exercises":
					
						break;
				}
				$contentsTemp[] = $content;
			}

			$pageTemp->setContents( $contentsTemp );
			$pages[] = $pageTemp;			
		}
		
		// Seta as páginas do objeto.
		$this->learningObject_->setPages( $pages );
	}
}
?>


